app.register.controller('MortgageCtrl', function($scope, $http, $rootScope,
		$route, MortgageSer, AppUtil, $q,$routeParams) {
	
	loadFlag($routeParams.id,$scope);
	//获取本地存储中的页码页数
	var page = AppUtil.GetLocalValue("mortgageTable_Page");
	var size = AppUtil.GetLocalValue("mortgageTable_Size");
	$scope.pagination = {
			totalItems :0,
			currentPage : page == undefined ? 1 : page,
			itemsPerPage : size == undefined ? 10 : size
	};
	
	$scope.SearchData = function() {
		$scope.postData={};
		// 点击查询按钮时,恢复页码为1
		$scope.pagination.currentPage = 1
		// 表单查询参数
		
		LoadList();
	};
	// 加载数据方法
	var LoadList = function() {
		if ($scope.postData == undefined)
			$scope.postData = {};
		var params = AppUtil.Params(".form_params");
		params = $.extend({}, params,$scope.selected);
		// 当有查询参数时,重置页数
		if (AppUtil.Length(params) > 0) {
			if($scope.entity_name != null){
				params['entity_name'] = $scope.entity_name;
			}
			$scope.postData.params = params;
		}
		// 筛选查询参数
		var search_opt = AppUtil.GetSearchOpt();
		if (search_opt.length > 0) {
			$scope.postData.search_opt = search_opt;
		}
		$scope.postData.page = $scope.pagination.currentPage;
		$scope.postData.size = $scope.pagination.itemsPerPage;
		// 加载数据
		MortgageSer.list($scope.postData).success(function(response) {
			AppUtil.remove_loading();
			$scope.pagination.totalItems = response.total;
			$scope.mortgages = response.data.data;
			$scope.sumData = response.data.sumData;
			setTimeout(check_table_tr,500);
		});
	}
	// 配置分页基本参数
	$scope.$watch('pagination.refresh',LoadList);

	AppUtil.LoadGroups();
	//加载下拉列表数据
	$http.get("mortgage/dicts").success(function(data){
		$scope.mechanism_types=data.data.mechanism_types;
		$scope.loan_types=data.data.loan_types;
		$scope.financial_institutions=data.data.financial_institutions;
		$scope.users=data.data.users;
	});
	// 导出
	$scope.ExportData = function() {
		AppUtil.ExportData($scope.postData);
	};
});

app.register.service('MortgageSer', function($http,AppUtil) {
	this.list = function(postData) {
		AppUtil.loading();
		//设置查询开始下标
		postData.start= (postData.page-1<0?0:postData.page-1)*postData.size;
		return $http.post('mortgage/list', postData).error(function() {
			AppUtil.remove_loading();
		});;
	};

});
